#! python3

# weather.py - Prints the weather for a location from the command line.

import json, requests, sys

# Compute location from command line arguments.eg: San Francisco, CA/beijing

if len(sys.argv) < 2:
	print('Usage: weather.py location')
	sys.exit()

location = ' '.join(sys.argv[1:])

# Download the JSON data from OpenWeatherMap.org's API.

apiId = "12b2817fbec86915a6e9b4dbbd3d9036"
url ='http://api.openweathermap.org/data/2.5/forecast/daily?q=%s&cnt=3&&appid=%s' % (location, apiId)
#url = "http://api.openweathermap.org/data/2.5/weather?q=beijing&units=metric&appid=%s" % (apiId)
        
response = requests.get(url)
print("code %s " % response.status_code)
print("web content %s " % (response.text))

try:
	#response.raise_for_status()

	# Load JSON data into a Python variable.

	weatherData = json.loads(response.text)

	# Print weather descriptions.

	w = weatherData['list']

	print('Current weather in %s:' % (location))
	print(w[0]['weather'][0]['main'], '-', w[0]['weather'][0]['description'])
	print()
	print('Tomorrow:')
	print(w[1]['weather'][0]['main'], '-', w[1]['weather'][0]['description'])
	print()
	print('Day after tomorrow:')
	print(w[2]['weather'][0]['main'], '-', w[2]['weather'][0]['description'])
except Exception:
	weatherData = json.loads(response.text)
	
	print("get weather error,with code:%s" % weatherData['cod'])
	print("message:%s" % weatherData['message'])
	
	

